﻿#region Usings
using System.Web.Mvc;
using System.Web.Security;

using BK.Security;
using BK.UI.Web.Models;
#endregion

namespace BK.UI.Web.Controllers
{
    public class MembersController : Controller
    {
        private IMembershipService _membershipService = new AccountMembershipService();
        private IFormsAuthenticationService _formsService = new FormsAuthenticationService();

        #region Instance Methods
        public ActionResult Login()
        {
            return View("Login");
        }

        [HttpPost]
        public ActionResult Login(LoginAccountViewModel model, string returnUrl)
        {
            if (ModelState.IsValid)
            {
                if (_membershipService.ValidateUser(model.UserName, model.Password))
                {
                    _formsService.SignIn(model.UserName, true);
                    if (Url.IsLocalUrl(returnUrl))
                    {
                        return Redirect(returnUrl);
                    }
                    return RedirectToAction("Index", "Home");
                }
                ModelState.AddModelError("", "The user name or password provided is incorrect.");
            }

            // If we got this far, something failed, redisplay form
            return View(model);
        }

        public ActionResult Logout()
        {
            _formsService.SignOut();

            return RedirectToAction("Index", "Home");
        }
        #endregion
    }
}